import { defineStore } from 'pinia'

import { login, logout, getUserDetail } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'
import type {
  IUserModel,
  IUserState,
  ILoginResponse,
  IUserInfoResponse,
} from '@/model/userModel'
import { ArgumentsType } from '@vueuse/core'
import { stringifyQuery } from 'vue-router'

export const state: IUserState = {
  token: getToken() || '',
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  routers: [],
  email: '',
  premission: [],
}

const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken(),
    name: '',
    avatar: '',
    email: '',
    routers: [] as any[],
    roles: [] as string[],
    permissions: [] as string[],
  }),

  actions: {
    /**
     * @description: 登录, 别用箭头函数，因为这里需要用到this指针！
     * @param {IUserInfo} userInfo 用户信息
     * @return {Promise<string>}
     */
    async login(userInfo: IUserModel): Promise<string> {
      const result: ILoginResponse = await login(userInfo)
      if (result.code === 200) {
        setToken(result.data.access_token)
        this.token = result.data.access_token
        return 'ok'
      } else {
        return Promise.reject(new Error(result.msg))
      }
    },

    /**
     * @description: 获取用户信息, 别用箭头函数，因为这里需要用到this指针！
     * @return {Promise<string>}
     */
    async getUserInfo(): Promise<string> {
      const userInfo: IUserInfoResponse = await getUserDetail()
      if (userInfo.code == 200) {
        const user = userInfo.user
        const avatar =
          user === undefined || user.avatar == null ? defAva : user.avatar

        if (userInfo.roles && userInfo.roles.length > 0) {
          // 验证返回的roles是否是一个非空数组
          this.roles = userInfo.roles
          this.permissions = userInfo.permissions
        } else {
          this.roles = ['ROLE_DEFAULT']
        }
        this.name = user.userName
        this.avatar = avatar
        return 'ok'
      } else {
        return Promise.reject(new Error(userInfo.msg))
      }
    },

    /**
     * @description: 退出系统,别用箭头函数，因为这里需要用到this指针！
     * @return {Promise<void>}
     */
    async logout(): Promise<string> {
      const result: any = await logout()
      if (result.code == 200) {
        // 清空用户的token信息
        this.token = ''
        // 清空用户角色信息
        this.roles = []
        // 清空用户权限信息
        this.permissions = []
        // 移除本地存储的token
        removeToken()
        return 'ok'
      } else {
        return Promise.reject(new Error(result.msg || ''))
      }
    },
  },
})

export default useUserStore
